package com.mocoop.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class SyncDaoImp implements SyncDao {
	private Context context;
	private String dbName;

	public SyncDaoImp(Context context, String dbName) {
		super();
		this.context = context;
		this.dbName = dbName;
	}

	public void createTable(String sql) {
		SQLiteDatabase database = null;
		try {
			database = SQLiteHelper.getDataBase(context, dbName);
			database.execSQL(sql);
		} catch (Exception e) {

		} finally {
			// database.close();
		}
	}

	public void addData(ContentValues cv, String tableName) {
		SQLiteDatabase database = null;
		try {

			database = SQLiteHelper.getDataBase(context, dbName);
			if (cv.get("localPK") == null
					|| "null".equals(cv.get("localPK").toString())) {
				cv.remove("localPK");
				cv.put("localPK", cv.get("jsonPK").toString());
			}

			int i = database.update(tableName, cv, "jsonPK = ?",
					new String[] { cv.get("jsonPK").toString() });
			if (i <= 0) {

				int k = -1;
				if (cv.get("localPK") != null
						&& !"null".equals(cv.get("localPK").toString())) {
					k = database.update(tableName, cv, "localPK = ?",
							new String[] { cv.get("localPK").toString() });
				}
				if (k <= 0) {
					database.insert(tableName, null, cv);
				}
			}

		} catch (Exception e) {
			e.printStackTrace();

		} finally {
			// database.close();
		}
	}

	public String findMaxTime(String tableName) {
		Cursor cursor = null;
		String maxTime = null;
		SQLiteDatabase database = null;
		try {
			database = SQLiteHelper.getDataBase(context, dbName);
			cursor = database.rawQuery("select max(lastupdatetime) from "
					+ tableName, null);
			if (cursor.moveToFirst()) {
				maxTime = cursor.getString(0);
			}

		} catch (Exception e) {

		} finally {
			try {
				if (cursor != null)
					cursor.close();
			} catch (Exception e) {
			}
		}
		return maxTime;
	}

	public void insertToSQLTable(Map<String, Object> map, String tableName) {
		SQLiteDatabase database = null;
		try {
			database = SQLiteHelper.getDataBase(context, dbName);
			ContentValues cv = new ContentValues();
			Iterator<String> it = map.keySet().iterator();
			while (it.hasNext()) {
				String key = it.next();
				if ("createsql".equals(key)) {
					continue;
				}
				cv.put(key, map.get(key).toString());
			}

			Long i = database.insert(tableName, "", cv);
			Log.d("RZZ", i + "");

		} catch (Exception e) {

		} finally {
			// database.close();
		}

	}

	public List<Map<String, String>> execute(String sql, String[] selectionArgs) {
		Cursor cursor = null;
		SQLiteDatabase database = null;
		List<Map<String, String>> list = new ArrayList<Map<String, String>>();
		try {
			database = SQLiteHelper.getDataBase(context, dbName);
			cursor = database.rawQuery(sql, selectionArgs);

			while (cursor.moveToNext()) {
				Map<String, String> map = new HashMap<String, String>();
				for (int i = 0; i < cursor.getColumnCount(); i++) {
					map.put(cursor.getColumnName(i), cursor.getString(i));
				}

				list.add(map);
			}

		} catch (Exception e) {
			Log.d("RZZ", sql + e.toString());
			Map<String, String> map = new HashMap<String, String>();
			map.put("Exception", e.getClass().getName());
			list.add(map);
		} finally {
			if (cursor != null)
				cursor.close();
		}
		return list;
	}

}
